package top.fzqblog.customview;

import android.animation.ValueAnimator;
import android.animation.ValueAnimator.AnimatorUpdateListener;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path.Direction;
import android.graphics.Path;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.AppCompatImageView;
import android.util.AttributeSet;

public class PipeImageViewView extends AppCompatImageView {

	private int view_width, view_height;

	private int anim_duration = 1000;

	private Paint paint;
	private Path path;

	private float radius;

	private Drawable drawable;

	private int x, y;

	public PipeImageViewView(Context context) {
		this(context, null);

	}

	public PipeImageViewView(Context context, AttributeSet attrs) {
		super(context, attrs);
		paint = new Paint();
		paint.setAntiAlias(true);
		paint.setColor(Color.CYAN);
		paint.setStyle(Style.FILL);

		path = new Path();
	}

	@Override
	protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
		view_width = right - left;
		view_height = bottom - top;
		super.onLayout(changed, left, top, right, bottom);
	}

	/**
	 * 获取控件宽度
	 * 
	 * @return
	 */
	public int getView_width() {
		return view_width;
	}

	/**
	 * 获取控件高度
	 * 
	 * @return
	 */
	public int getView_height() {
		return view_height;
	}

	/**
	 * 显示Drawable，从中间点开始扩散
	 * 
	 * @param drawableRes
	 */
	public void show(int drawableRes) {
		show(view_width / 2, view_height / 2, drawableRes);
	}

	/**
	 * 显示Drawable，从指定点扩散
	 * 
	 * @param x
	 * @param y
	 * @param drawableRes
	 */
	public void show(int x, int y, int drawableRes) {
		show(x, y, getResources().getDrawable(drawableRes));
	}

	/**
	 * 显示Bitmap，从中间点扩散
	 * 
	 * @param bm
	 */
	public void show(Bitmap bm) {
		show(view_width / 2, view_height / 2, bm);
	}

	/**
	 * 显示Bitmap，从指定点扩散
	 * 
	 * @param x
	 * @param y
	 * @param bm
	 */
	@SuppressWarnings("deprecation")
	public void show(int x, int y, Bitmap bm) {
		show(x, y, new BitmapDrawable(bm));
	}

	/**
	 * 显示Drawable,从中间扩散
	 * 
	 * @param drawable
	 */
	public void show(Drawable drawable) {
		show(view_width / 2, view_height / 2, drawable);
	}

	/**
	 * 显示Drawable，从指定点扩散
	 * 
	 * @param x
	 * @param y
	 * @param drawable
	 */
	public void show(int x, int y, Drawable drawable) {
		this.x = x;
		this.y = y;
		this.drawable = drawable;
		if (drawable == null)
			return;
		drawable.setBounds(0, 0, view_width, view_height);
		float leftTop = (float) Math.sqrt(x * x + y * y);
		float rightTop = (float) Math.sqrt((view_width - x) * (view_width - x) + y * y);
		float leftBottom = (float) Math.sqrt(x * x + (view_height - y) * (view_height - y));
		float rightBottom = (float) Math.sqrt((view_width - x) * (view_width - x) + (view_height - y) * (view_height - y));
		final float end = (float) Math.max(Math.max(leftTop, rightTop), Math.max(leftBottom, rightBottom));
		ValueAnimator anim = ValueAnimator.ofFloat(0, end);
		anim.setDuration(anim_duration);
		anim.addUpdateListener(new AnimatorUpdateListener() {

			@Override
			public void onAnimationUpdate(ValueAnimator arg0) {
				float value = (Float) arg0.getAnimatedValue();
				radius = value;
				int alpha = (int) (value / end * 255);
				PipeImageViewView.this.drawable.setAlpha(alpha);
				invalidate();
			}
		});
		anim.start();

	}

	@Override
	protected void onDraw(Canvas canvas) {
		if (drawable != null) {
			path.reset();
			path.addCircle(x, y, radius, Direction.CW);
			canvas.save();
			canvas.clipPath(path);
			drawable.draw(canvas);
			canvas.restore();
		}
	}

}
